package com.lantosec.hmrp.modules.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.kit.tree.TreeNodeVo;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.system.entity.SysMenu;
import com.lantosec.hmrp.modules.system.service.ISysMenuService;
import com.lantosec.hmrp.modules.system.vo.SysMenuVO;
import com.lantosec.hmrp.modules.system.vo.SysMenusVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 *
 * 系统菜单表 控制器
 *
 * @author AutoCode
 * @since 2020-02-12
 */
@Api(value = "系统菜单表控制器", tags = "系统菜单表控制器")
@RestController
@RequestMapping("/system/menu")
@Log4j2
public class SysMenuController extends BaseController {
    @Autowired
    private ISysMenuService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<SysMenuVO>> all() {
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<SysMenuVO> get(@PathVariable(name = "id") String id) {
        return Result.ok(entity2VO(service.getById(id)));
    }

    @GetMapping("getAll")
    @ApiOperation(value = "查询所有的菜单")
    public Result<List<SysMenusVO>> getAll(@ApiParam(value = "menuId") @RequestParam(name="menuId",required = false) String menuId,
                                           @ApiParam(value = "部门ID") @RequestParam(name="deptId",required = false) String deptId,
                                           @ApiParam(value = "岗位ID") @RequestParam(name="positionId",required = false) String positionId) {
        return Result.ok(service.getAll(menuId,deptId,positionId));
    }


    @GetMapping("getMenuTree")
    @ApiOperation(value = "获得菜单树")
    public Result<List<TreeNodeVo>> getMenuTree() {
        return Result.ok(service.menuTree());
    }

    @GetMapping("getMenuTree/{id}")
    @ApiOperation(value = "根据指定ID查询对应的菜单树")
    public Result<List<TreeNodeVo>> getMenuTree(@PathVariable(name = "id") String id) {
        return Result.ok(service.menuTree(id));
    }

    @GetMapping("getMenuById")
    @ApiOperation(value = "查询当前菜单下的子菜单")
    public Result<List<SysMenuVO>> getMenuById(@ApiParam(value = "ID", required = true) @RequestParam(name="id") String id){
        List<SysMenu> sysMenuS = service.getMenuPage(id);
        return Result.ok(entity2VO(sysMenuS));
    }

    @PostMapping("insertMenuInfo")
    @ApiOperation(value = "新增数据")
    public Result<String> create(@ApiParam(value = "系统菜单表VO对象", required = true) @RequestBody @Validated SysMenuVO vo, BindingResult bindingResult) {
        validationHandler(bindingResult);
        SysMenu entity = vo.convertToEntity();
        String menuId = service.queryMaxmenuId(vo.getParentId());
        entity.setMenuId(menuId);
        service.save(entity);
        return Result.ok(entity.getMenuId());
    }

    @PutMapping("updateMenuInfo")
    @ApiOperation(value = "修改数据")
    public Result<Void> updateMenuInfo(@ApiParam(value = "系统菜单表VO对象", required = true) @RequestBody SysMenuVO vo){
        SysMenu entity = vo.convertToEntity();
        Boolean update = service.updateById(entity);
        if(update){
            return Result.ok();
        }else {
            return Result.fail("操作失败");
        }
    }

    @DeleteMapping(value = "deleteMenuInfo/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> deleteMenuInfo(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids) {
        service.deleteMenuInfo(ids);
        return Result.ok();
    }

    @PutMapping("updateOrder")
    @ApiOperation(value = "更新菜单排序")
    public Result<Void> updateOrder(@ApiParam(value = "菜单ID", required = true) @RequestParam(name = "id") String id,
                                    @ApiParam(value = "排序值", required = true) @RequestParam(name = "menuOrder") Integer menuOrder) {
        service.updateOrder(id,menuOrder);
        return Result.ok();
    }
}
